package in.abip.chisame.model;

import in.abip.chisame.R;
import android.content.res.Resources;

/**
 * Light record.
 * 
 * @author Julot.
 * @since Saturday, January 22nd, 2011.
 */
public class Light extends AbstractChannel implements IListItem, ISwitch {
	public static Light model() {
		return new Light();
	}
	/** @return string type name */
	public String getTypeName() {
		switch (typeId) {
		case 1: return "Spotlight";
		case 2: return "Incandescent";
		case 3: return "Fluorescent";
		default: return "Chandelier";
		}
	}
	
	/** @return value for toogle operation */
	private int getToggleValue() {
		if (_toggleValue == 0) return 0;
		if (_toggleValue >= MAX_VALUE) {
			return MAX_VALUE;
		}
		return _toggleValue;
	}
	
	public Integer getValue() {
		if (!hasRange && _value > 0) return MAX_VALUE;
		return _value;
	}
	
	public void setValue(Integer value) {
		_virgin = false;
		if (value > MAX_VALUE) {
			_value = MAX_VALUE;
		}
		else if (value < 0) _value = 0;
		else _value = value;
		_toggleValue = _value;
	}
	
	/**
	 * Get list icon drawable resource according to type.
	 * @return {@link Resources}
	 */
	public int getListIconResource() {
		switch (typeId) {
		case 1:
			if (getValue() == 0) return R.drawable.ic_list_light_type_1;
			else return R.drawable.ic_list_light_type_1_on;
		case 2:
			if (getValue() == 0) return R.drawable.ic_list_light_type_2;
			else return R.drawable.ic_list_light_type_2_on;
		case 3:
			if (getValue() == 0) return R.drawable.ic_list_light_type_3;
			else return R.drawable.ic_list_light_type_3_on;
		default:
			if (getValue() == 0) return R.drawable.ic_list_light_type_4;
			else return R.drawable.ic_list_light_type_4_on;
	    }
	}
	
	public String getListText() {
		return ((name == null) || (name.equals(""))) ? getTypeName() : name;
	}
	
	/**
	 *  Toggle value to 0 and max value.
	 */
	public void toggle() {
		_virgin = false;
		if (getValue() > 0) {
			_value = 0;
		}
		else {
			_value = getToggleValue() == 0 ? MAX_VALUE : getToggleValue();
		}
	}

	public String getValueName() {
		return getValueName(getValue());
	}
	
	public String getValueName(Integer value) {
		if (_virgin || value.equals(null)) return "Unknown";
		if (value == 0) return "Off";
		if (hasRange) return value + "%";
		return "On";
	}
}
